<?php

namespace App\Http\Controllers\Api\Utils;

use App\Handlers\ImageUploadHandler;
use App\Http\Controllers\Controller;
use App\Models\Recruit;
use App\Models\RecruitCategory;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Vtiful\Kernel\Excel as XlsWriter;

class DealDataController extends Controller
{
    public function mockRecruit1()
    {
        $fileName = 'app/public/recruit/boss-java-武昌区.json';
        $filePath = storage_path($fileName);
        $jobs = file_get_contents($filePath);
        $jobs = json_decode($jobs, true);

        foreach ($jobs as $job) {
            $data[] = [
                'user_id' => mt_rand(1, 12),
                'province' => '湖北省',
                'city' => '武汉市',
                'district' => '武昌区'
            ];
        }
    }

    public function getRoute(Request $request)
    {
        $url = substr(url()->current(), strlen(url()->previous())); // 这个会将路由前面的那个 / 获取到
        $url2 = $request->route()->uri(); // 这个只会获取到路由名称
        dump($url);
        dump($url2);
    }

    public function mockRecruitTitle()
    {
        $recruits = Recruit::query()
            ->where('id', '>=', 1083)
            ->limit(1)->get();
        foreach ($recruits as $recruit) {
            $recruitDesc = $recruit->recruit_desc;
            $pos = strpos($recruitDesc, "\n");

            if ($pos !== false) {
                $recruitTitle = substr($recruitDesc, 0, $pos);

            } else {
                echo "未找到换行符";
            }
        }
    }

    public function planningArea()
    {
        /*$file = storage_path('app/public/recruit/武汉市行政区（商圈）名单.txt');
        $areas = file_get_contents($file);
        return $areas;*/
    }

    public function updateRecruit()
    {
        $recruitIds = Recruit::query()
            //->where('id', '>=', 1083)
            //->whereNotNull('longitude')
            ->where('user_id', '=', 0)
            ->pluck('id')
            ->toArray();
        $userIds = User::query()->pluck('id')->toArray();
        foreach ($recruitIds as $recruitId) {
            $userIdKey = array_rand($userIds);
            $userIdValue = $userIds[$userIdKey];
            Recruit::query()->where('id', '=', $recruitId)->update(['user_id' => $userIdValue]);
        }
    }

    public function mockRecruit()
    {
        $filePath = 'app/public/recruit';
        $storagePath = storage_path($filePath);

        $config = [
            'path' => $storagePath
        ];
        $excel  = new Xlswriter($config);
        $openFileName = "jobs.xlsx";
        $xlsxDatas = $excel->openFile($openFileName)->openSheet()->getSheetData();
        $recruits = [];
        foreach ($xlsxDatas as $key => $xlsData) {
            if ($key) {
                $recruits[] = [
                    'province'          => $xlsData[0],
                    'city'              => $xlsData[1],
                    'district'          => $xlsData[2],
                    'address'           => $xlsData[3],
                    'address_detail'    => $xlsData[4],
                    'contact_name'      => $xlsData[5],
                    'contact_phone'     => $xlsData[6],
                    'recruit_desc'      => $xlsData[7],
                    'recruit_cate_name' => $xlsData[10],
                    'salary_info'       => $xlsData[11],
                ];
            }
        }
        Recruit::query()->insert($recruits);
    }

    public function dealRecruitCate()
    {
        $castes = $this->getCate();
        $id = 12;
        $name = '传单员';
        foreach ($castes as $cate) {
            $data = [
                'name' => $cate,
                'parent_id' => $id,
                'level' => 2,
            ];
            $insertId = RecruitCategory::query()->insertGetId($data);
            RecruitCategory::query()->where('id', $insertId)->update([
                'full_id_path' => $id .',' . $insertId,
                'full_name_path' => $name .',' . $cate,
            ]);
        }
    }

    public function getCate()
    {
        return [
            "理发店传单员",
            "网咖发传单员",
            "夜宵摊传单员",
            "奶茶店传单员",
            "烧烤店传单员",
            "汽水店传单员",
            "零食店传单员",
            "茶叶店传单员",
            "早餐店传单员",
            "汽车店传单员",
            "汉堡店传单员",
            "药店传单员",
            "美容店传单员",
            "汽车店传单员",
            "水果店传单员",
            "咖啡店传单员",
            "首饰店传单员"
        ];
    }

    public function dealCity()
    {
        $cities = $this->getCityData();
        foreach ($cities as $key => $city) {
            $data[] = [
                'address' => $city['address'],
                'address_detail' => $city['address_detail'],
                'latitude' => $city['latitude'],
                'longitude' => $city['longitude'],
                'contact_phone' => $city['contact_phone'],
                'contact_name' => ($key + 1). "先生",
                'recruit_desc' => '洗碗工',
                'recruit_name_path' => '洗碗工',
                'recruit_id_path' => 10,
                'salary_info' => '{"salary": "30", "workHour": "3", "salaryType": "hourSalary"}'
            ];
        }
        Recruit::query()->insert($data);
    }

    public function getCityData()
    {
        return [
          ['address' => '凯德广场.1818', 'address_detail' => '湖北省武汉市武昌区中北路109号', 'latitude'=> 30.552965, 'longitude' => 114.340008, 'contact_phone' => '027-87811818'],
          ['address' => '龙源国际广场-A座', 'address_detail' => '湖北省武汉市武昌区中北路龙源国际广场', 'latitude'=> 30.554564, 'longitude' => 114.342437, 'contact_phone' => '027-87811818'],
          ['address' => '岳家嘴社区', 'address_detail' => '湖北省武汉市武昌区水果湖街道', 'latitude'=> 30.576073, 'longitude' => 114.355049, 'contact_phone' => '027-87811818'],
          ['address' => '东亭·新嘉源', 'address_detail' => '湖北省武汉市洪山区欢乐大道269号', 'latitude'=> 30.588290, 'longitude' => 114.373656, 'contact_phone' => '027-87811818'],
          ['address' => '中碳登大厦', 'address_detail' => '湖北省武汉市武昌区中北路青鱼嘴', 'latitude'=> 30.564552, 'longitude' => 114.349356, 'contact_phone' => '027-87811818'],
          ['address' => '洪山广场', 'address_detail' => '湖北省武汉市武昌区水果湖街办事处中北路特1号', 'latitude'=> 30.545048, 'longitude' => 114.337111, 'contact_phone' => '027-87811818'],
          ['address' => '中南路', 'address_detail' => '湖北省武汉市武昌区', 'latitude'=> 30.539848, 'longitude' => 114.334075, 'contact_phone' => '027-87811818'],
          ['address' => '武昌站', 'address_detail' => '湖北省武汉市武昌区中山路534号', 'latitude'=> 30.528671, 'longitude' => 114.317792, 'contact_phone' => '027-87811818'],
          ['address' => '华中小龟山金融文化公园', 'address_detail' => '湖北省武汉市武昌区紫沙路203号', 'latitude'=> 30.550592, 'longitude' => 114.324325, 'contact_phone' => '027-87811818'],
          ['address' => '武汉市武昌区人民政府', 'address_detail' => '湖北省武汉市武昌区中山路307号', 'latitude'=> 30.554147, 'longitude' => 114.316407, 'contact_phone' => '027-87811818'],
          ['address' => '湖北省博物馆', 'address_detail' => '湖北省武汉市武昌区东湖路160号', 'latitude'=> 30.561259, 'longitude' => 114.365321, 'contact_phone' => '027-87811818'],
          ['address' => '武汉大学中南医院', 'address_detail' => '湖北省武汉市武昌区东湖路169号', 'latitude'=> 30.553497, 'longitude' => 114.352844, 'contact_phone' => '027-87811818'],
          ['address' => '银泰创意城', 'address_detail' => '湖北省武汉市洪山区珞喻路35号', 'latitude'=> 30.526416, 'longitude' => 114.356581, 'contact_phone' => '027-87811818'],
          ['address' => '武汉电脑城', 'address_detail' => '湖北省武汉市洪山区珞喻路201号', 'latitude'=> 30.523303, 'longitude' => 114.363939, 'contact_phone' => '027-87811818'],
          ['address' => '福星惠誉·群星城', 'address_detail' => '湖北省武汉市武昌区徐东大街120号', 'latitude'=> 30.584086, 'longitude' => 114.363939, 'contact_phone' => '027-87811818'],
          ['address' => '徐家棚匠心城', 'address_detail' => '湖北省武汉市武昌区徐东大街8号', 'latitude'=> 30.592024, 'longitude' => 114.339596, 'contact_phone' => '027-87811818'],
          ['address' => '铁机东方红建材大市场', 'address_detail' => '湖北省武汉市洪山区铁机路581号', 'latitude'=> 30.594989, 'longitude' => 114.366412, 'contact_phone' => '027-87811818']
        ];
    }

    /**
     * @param Request $request
     * @param ImageUploadHandler $uploader
     * @return JsonResponse
     */
    public function uploadImage(Request $request, ImageUploadHandler $uploader): JsonResponse
    {
       /* dump(base_path('storage'));
        $absolutePath = storage_path();
        dump($absolutePath);
        $relativePath = str_replace(base_path(), '', $absolutePath);
        dd($relativePath);*/
        // 初始化返回数据，默认是失败的
        $result = [ 'save_path' => '', 'access_path' => ''];
        // 判断是否有上传文件，并赋值给 $file
        if ($file = $request->upload_image) {
            // 保存图片到本地
            $result = $uploader->save($request->upload_image, 'banner', 2, 1024);
        }
        return result(0, '上传成功', $result);
    }
}
